//
//  detailsClassicJokeViewController.m
//  HaHa
//
//  Created by 姚君泽 on 15/9/23.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "detailsClassicJokeViewController.h"

@interface detailsClassicJokeViewController ()

@property (nonatomic, strong)NSMutableArray *commentsArr;

@property (nonatomic, strong)UIImageView *commenterImageView;

@property (nonatomic, strong)UILabel *commenterLabel;

@property (nonatomic, strong)UILabel *timeLabel;

@property (nonatomic, strong)UILabel *commentLabel;

@property (nonatomic)CGFloat *High;
@end

@implementation detailsClassicJokeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor yellowColor];
    [self addTableView];
    // Do any additional setup after loading the view.
    [self reloadGroupID];
}
-(NSMutableArray *)commentsArr
{
    if (!_commentsArr) {
        _commentsArr = [[NSMutableArray alloc] init];
    }
    return _commentsArr;
}
-(void)reloadGroupID
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@",KFirstUrl,self.groupID,KSecondUrl];
   
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSArray *recentArr = [dataDic objectForKey:@"recent_comments"];
        NSArray *topArr = [dataDic objectForKey:@"top_comments"];
        for (NSDictionary *topDic in topArr) {
            CommentsModel *commentModel = [CommentsModel shareModelWithDic:topDic];
            [self.commentsArr addObject:commentModel];
        }
        for (NSDictionary *recentDic in recentArr) {
            CommentsModel *recentModel = [CommentsModel shareModelWithDic:recentDic];
            [self.commentsArr addObject:recentModel];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


-(void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, kScreenWdth - 20, kScreenHeight - 49)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self addheaderView:self.tableView];
    [self.view addSubview:self.tableView];
    [self setModel];
}
-(void)addheaderView:(UITableView *)tableView
{
    if (self.todayModel == nil && self.weekModel == nil) {
         self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWdth, 60 + [self YYHreturnStringHeight:self.classModel.content Wdth:(kScreenWdth - 90) Font:17.0])];
    }else if (self.classModel == nil && self.weekModel == nil){
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWdth, 60 + [self YYHreturnStringHeight:self.todayModel.content Wdth:(kScreenWdth - 90) Font:17.0])];
    }else
    {
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWdth, 60 + [self YYHreturnStringHeight:self.weekModel.text Wdth:(kScreenWdth - 90) Font:17.0])];
    }
    
   self.commenterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    self.commenterImageView.layer.masksToBounds = YES;
    self.commenterImageView.layer.cornerRadius = self.commenterImageView.frame.size.height / 2;
    
    [self.tableView.tableHeaderView addSubview:self.commenterImageView];
    
    self.commenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 170, 40)];
    self.commenterLabel.textColor = [UIColor grayColor];
    
    [self.tableView.tableHeaderView addSubview:self.commenterLabel];
    
//    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWdth - 120, 10, 100, 40)];
//    self.timeLabel.font = [UIFont boldSystemFontOfSize:11];
//    self.timeLabel.textColor = [UIColor grayColor];
//    self.timeLabel.backgroundColor = [UIColor purpleColor];
//    [self.tableView.tableHeaderView addSubview:self.timeLabel];
    
    self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 60, kScreenWdth - 90, 240)];
    
    [self.tableView.tableHeaderView addSubview:self.commentLabel];
    
}
- (CGFloat)YYHreturnStringHeight:(NSString *)string Wdth:(CGFloat)Wdth Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Wdth, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    
    //NSLog(@"%f",frame.size.height);
    
    return frame.size.height;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *string = @"热门评论";
    return string;
}
-(void)setModel
{
    if (self.todayModel == nil && self.weekModel == nil) {
       
        [self.commenterImageView sd_setImageWithURL:[NSURL URLWithString:self.classModel.user[@"avatar_url"]]];
        self.commenterLabel.text = self.classModel.user[@"name"];
        self.commentLabel.text = self.classModel.content;
    }else if (self.classModel == nil && self.weekModel == nil){
        [self.commenterImageView sd_setImageWithURL:[NSURL URLWithString:self.todayModel.userDic[@"avatar_url"]]];
        self.commenterLabel.text = self.todayModel.userDic[@"name"];
        self.commentLabel.text = self.todayModel.content;
    }else
    {
        [self.commenterImageView sd_setImageWithURL:[NSURL URLWithString:self.weekModel.essay_user[@"avatar_url"]]];
        self.commenterLabel.text = self.weekModel.essay_user[@"name"];
        self.commentLabel.text = self.weekModel.text;
    }
    [self setHeaderFrame];
}
-(void)setHeaderFrame
{
    self.commentLabel.numberOfLines = 0;
    self.commentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [self.commentLabel sizeThatFits:CGSizeMake(self.commentLabel.frame.size.width, MAXFLOAT)];
    self.commentLabel.frame = CGRectMake(self.commentLabel.frame.origin.x, self.commentLabel.frame.origin.y, self.commentLabel.frame.size.width, size.height);
//    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kScreenWdth, self.commentLabel.frame.origin.y + self.commentLabel.frame.size.height);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
        return self.commentsArr.count;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.commentCell.commentLabel.frame.origin.y + self.commentCell.commentLabel.frame.size.height + 10;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
            self.commentCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!self.commentCell) {
            self.commentCell = [[commentJokeTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
        }
    CommentsModel *commentModel = [[CommentsModel alloc] init];
    commentModel = [self.commentsArr objectAtIndex:indexPath.row];
    [self.commentCell setValueWithModel:commentModel];
    
        return self.commentCell;
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
